package com.fndsoft.zheshangmeap.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.text.TextUtils;

import com.fndsoft.zheshangmeap.component.colog.COLog;

/**
 * @author carlos carlosk@163.com
 * @version 创建时间：2012-9-17 下午4:59:46 类说明
 */

public class DateUtil extends BaseUtils {
	/**
	 * 日期转换成字符串
	 * 
	 * @auther david 2013-11-23 下午3:06:06 String
	 */
	public static String formatDate(Date date) {
		if (null == date) {
			return "";
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd",
				Locale.CHINA);
		String day = format.format(date);
		return day;
	}

	public static String formatDateAndTime(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);
		String day = format.format(date);
		return day;
	}

	/**
	 * 根据秒数返回 12:34格式的文字
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年4月15日 上午2:08:12
	 */
	public static String getTimeMintueAndSecondString(long time) {
		int mintue = (int) time / 60;
		int second = (int) time % 60;
		String result = "";
		if (mintue < 10) {
			result += "0";
		}
		result += mintue+":";
		if (second < 10) {
			result += "0";
		}
		result += second;
		return result;
	}

	/**
	 * 日期转换成字符串
	 * 
	 * @auther david 2013-11-23 下午3:06:06 String
	 */
	public static String formatDate(Date date, String format1) {
		SimpleDateFormat format = new SimpleDateFormat(format1, Locale.CHINA);
		String day = format.format(date);
		return day;
	}

	/**
	 * 文字转换成日期
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：Mar 11, 2014 12:30:55 AM
	 * @param dateStr
	 * @return
	 */
	public static Date formatDate(String dateStr) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd",
				Locale.CHINA);
		try {
			return format.parse(dateStr);
		} catch (ParseException e) {
		}
		return null;
	}

	public static Date formatTimeAndDate(String dateStr) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);
		try {
			return format.parse(dateStr);
		} catch (ParseException e) {
		}
		return null;
	}

	/**
	 * 格式化时间
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：Mar 9, 2014 11:47:43 AM
	 * @param date
	 * @return
	 */
	public static String formatTime(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.CHINA);
		String day = format.format(date);
		return day;
	}

	/**
	 * 数据转换
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年3月3日 下午5:47:09
	 * @return
	 */
	public static Date formatDate(int year, int month, int day, int hourOfDay,
			int minute) {
		Calendar calendar = Calendar.getInstance();
		if (year < 0) {
			year = calendar.get(Calendar.YEAR);
		}
		if (month < 0) {
			month = calendar.get(Calendar.MONTH);
		}
		if (hourOfDay < 0) {
			hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
		}
		if (minute < 0) {
			minute = calendar.get(Calendar.MINUTE);
		}
		calendar.set(year, month, day, hourOfDay, minute);
		return new Date(calendar.getTimeInMillis());
	}

	/**
	 * 是否是以前的时间
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2012-9-17 下午5:01:18
	 * @param dateString
	 * @return
	 */
	public static boolean isBygone(String dateString) {

		return getTimeByDateString(dateString) < System.currentTimeMillis();
	}

	/**
	 * 根据日期字符串获取时间
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2012-9-18 上午10:56:50
	 * @param dateString
	 * @return
	 */
	public static long getTimeByDateString(String dateString) {
		if (TextUtils.isEmpty(dateString)) {
			return System.currentTimeMillis();
		}
		Calendar calendar = Calendar.getInstance();
		String timeString = calendar.get(Calendar.YEAR) + "年" + dateString;
		System.out.println("time = " + timeString);
		DateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm", Locale.CHINA);
		Date date = null;
		try {
			date = sdf.parse(timeString);
		} catch (ParseException e) {
			sdf = new SimpleDateFormat("yyyy年MM-dd hh:mm", Locale.CHINA);
			try {
				date = sdf.parse(timeString);
			} catch (ParseException e1) {
			}
		}
		if (null == date) {
			return System.currentTimeMillis();
		}
		return date.getTime();
	}

	/**
	 * 获取当前的时间
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2013-8-28 下午1:17:25
	 */
	public static String getCurrentTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss",
				Locale.CHINA);
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		String str = formatter.format(curDate);
		return str;
	}

	/**
	 * 获取当前的日期和时间
	 * 
	 * @author Roy
	 * @version 创建时间:2013-10-15上午9:29:52
	 * @return
	 */
	public static String getCurrentDateAndTime() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);
		Date date = new Date(System.currentTimeMillis());// 获取当前时间
		String time = format.format(date);
		return time;
	}

	/**
	 * 获取当前天的数据
	 * 
	 * @auther david 2013-11-23 下午3:06:06 String
	 */
	public static String getCurrentDateAndDay() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd",
				Locale.CHINA);
		Date date = new Date(System.currentTimeMillis());// 获取当前日期
		String day = format.format(date);
		return day;
	}

	/**
	 * 这个方法是用来将日期类型从long转为 String类型
	 * 
	 * @auther david 2013-11-1 上午10:34:26 String
	 */
	public static String getDateTimeByLongDate(long dateLong) {
		if (0 >= dateLong) {
			return "";
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);
		java.util.Date date = new Date(dateLong);
		String dateString = sdf.format(date);
		return dateString;
	}

	/**
	 * 将总时间转换为时间格式显示
	 * 
	 * @author liujiaqi
	 * @creation time 2014-3-7上午11:10:09
	 * @version
	 * @param countSecond
	 *            (秒)
	 * @return
	 */
	public static String getCountTimeByLongDate(int countSecond) {
		int hour = 0;
		int minute = 0;
		int second = 0;
		hour = countSecond / 3600;
		minute = (countSecond % 3600) / 60;
		second = (countSecond % 3600) % 60;

		COLog.d("hour" + hour);
		COLog.d("minute:" + minute);
		COLog.d("second:" + second);

		String strHour = hour > 0 ? (hour > 9 ? hour + "" : "0" + hour) : "";
		String strMinute = minute > 9 ? minute + "" : "0" + minute;
		String strSecond = second > 9 ? second + "" : "0" + second;
		return ValueUtil.isStrNotEmpty(strHour) ? strHour + ":" + strMinute
				+ ":" + strSecond : strMinute + ":" + strSecond;
	}

	public static void main(String[] args) {
		System.out.println(DateUtil.getCountTimeByLongDate(1));
	}

	/**
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年5月3日 下午6:52:03
	 */
	public static String formatDateByString(String title1) {
		if (ValueUtil.isStrEmpty(title1)) {
			return "";
		}
		Date date = DateUtil.formatDate(title1);
		if (null == date) {
			date = new Date();
		}
		title1 = DateUtil.formatDate(date);
		return title1;
	}

}
